//let testurl= "http://123.57.21.176:8090/ligezhuang/";
//let testurl = "http://192.168.0.160:9006/";
let judge_id = sessionStorage.getItem("post_id");
let realName = sessionStorage.getItem("realname");
let user_idd = sessionStorage.getItem("user_id");
let token2 = sessionStorage.getItem("token");
let authList = sessionStorage.getItem("authList");
console.log(authList);

if (judge_id == "1") {
    console.log("管理")
}
if (judge_id == "2") {
    console.log("普通")
}

//权限
function rules(){
    let b = authList.split(","); // 将字符串按照","分割，存入数组b.
    $.each(b, function (index, value) {
        $(".quan_xian").each(function () {
            if ($(this).attr("rules") == value) {
                $(this).show();
            }
            else{
                
            }
        })
    })
}

$(function () {
//权限
rules();
    $("#name").append(realName);
    // backup
    $("#backup").click(function () {
        window.location.href = "index.html";
    });
    $(".flexMenu").click(function () {
        $(this).addClass("menu-bg");
        $(".flexMenu").not($(this)).removeClass("menu-bg");
    });
    //person settings
    let html = "<div style=\"width:900px;z-index: 1300!important;\" class=\"am-popup\" id=\"settings\">\n" +
        "  <div class=\"am-popup-inner\">\n" +
        "    <div class=\"am-popup-hd\">\n" +
        "      <h4 class=\"am-popup-title\">个人设置</h4>\n" +
        "      <span data-am-modal-close\n" +
        "            class=\"am-close\">&times;</span>\n" +
        "    </div>\n" +
        "    <div class=\"am-popup-bd\">\n" +
        "      <div class=\"am-tabs\" data-am-tabs>\n" +
        "  <ul class=\"am-tabs-nav am-nav am-nav-tabs\">\n" +
        "    <li class=\"am-active\"><a href=\"#tab1\">个人设置</a></li>\n" +
        "    <li><a href=\"#tab2\">修改密码</a></li>\n" +

        "  </ul>\n" +
        "\n" +
        "  <div class=\"am-tabs-bd\">\n" +
        "    <div class=\"am-tab-panel am-fade am-in am-active\" id=\"tab1\">\n" +
        "             <form class=\" am-form-horizontal\" method=\"post\" enctype=\"multipart/form-data\" id=\"setform\">\n" +
        "                <div class=\"am-u-lg-12\">\n" +
        "                    <div class=\"wordTable\">\n" +
        "                        \n" +
        "                        <h1>个人设置</h1>\n" +
        "                        \n" +
        "                        <table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" >\n" +
        "                            <tbody>\n" +
        "                                <tr>\n" +
        "                                    <td width=\"81\">姓&nbsp;名<span class=\"redStars\">*</span></td>\n" +
        "                                    <td  colspan=\"2\" ><input type=\"text\" readonly id=\"setrealname\"  class=\"txtcss3\"></td>\n" +
        "                                    <td>账号<span class=\"redStars\">*</span></td>\n" +
        "                                    <td colspan=\"2\" ><input readonly type=\"text\" id=\"setusername\"  class=\"txtcss3\"></td>\n" +
        
        "                                </tr>\n" +
        "                                <tr>\n" +
        "                                    <td>所属单位</td>\n" +
        "                                    <td colspan=\"2\" ><input readonly type=\"text\"  class=\"txtcss3\" id=\"setorgan\"><input type=\"hidden\" id=\"set_dw\"></td>\n" +
        "                                    <td>现任职务<span class=\"redStars\">*</span></td>\n" +
        "                                    <td colspan=\"2\"><input type=\"text\" id=\"setpresasg\" style=\"width:180px\" class=\"txtcss3\"></td>\n" +
        "                                </tr>\n" +
        "                                <tr>\n" +
       "                                     <td>身份<span class=\"redStars\">*</span></td>\n" +
        "                                    <td colspan=\"2\"><input readonly type='text' id=\"setshenfen\"  class=\"txtcss3\"><input type=\"hidden\" id=\"set_sf\"></td>\n" +
        "                                    <td>联系方式<span class=\"redStars\">*</span></td>\n" +
        "                                    <td colspan=\"2\"><input type=\"text\" id=\"setphone\" class=\"txtcss3\" style=\"width:150px\"></td>\n" +
        "                                    \n" +
        "                                </tr>\n" +
        
        "                               \n" +
        "                            </tbody>\n" +
        "                        </table>\n" +
        "\n" +
        "                        <div style=\"margin-top: 30px\">\n" +
        "                            <button type=\"button\"  id=\"setgo\" class=\"btnImg70\">确定</button>\n" +
        // "                            <button type=\"reset\" class=\"btnImg60\">重置</button>\n" +
        "                            <button type=\"button\" data-am-modal-close class=\"btnImg60\">取消</button>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                </div>\n" +
        "            </form>\n" +
        "    </div>\n" +
        "    <div class=\"am-tab-panel am-fade\" id=\"tab2\">\n" +
        "     <form class=\"am-form am-form-horizontal\">\n" +
        "  <div class=\"am-form-group\">\n" +
        "    <label  class=\"am-u-sm-2 am-form-label\">新密码</label>\n" +
        "    <div class=\"am-u-sm-10\">\n" +
        "      <input type=\"password\" id=\"setpsd\" placeholder=\"新密码\">\n" +
        "    </div>\n" +
        "  </div>\n" +
        "\n" +
        "  <div class=\"am-form-group\">\n" +
        "    <label class=\"am-u-sm-2 am-form-label\">确认密码</label>\n" +
        "    <div class=\"am-u-sm-10\">\n" +
        "      <input type=\"password\" id=\"setpsd2\" placeholder=\"确认密码\">\n" +
        "    </div>\n" +
        "  </div>\n" +
        "  <div class=\"am-form-group\">\n" +
        "    <div class=\"am-u-sm-10 am-u-sm-offset-2\">\n" +
        "      <button type=\"button\" class=\"am-btn am-btn-sm am-btn-default\" id=\"setpassword\">确认</button>\n" +
        "      <button type=\"button\" class=\"am-btn am-btn-sm am-btn-primary\" data-am-modal-close>取消</button>\n" +
        "    </div>\n" +
        "  </div>\n" +
        "</form>\n" +
        "    </div>\n" +

        "  </div>\n" +
        "</div>\n" +
        "    </div>\n" +
        "  </div>\n" +
        "</div>"
    $("#personSettings").html(html);
    autoLeftNav();
    $(window).resize(function () {
        autoLeftNav();
        // console.log($(window).width())
    });

    $("#setpassword").click(function () {
        let oldpsd = $("#setpsd").val();
        let newpsd = $("#setpsd2").val();
        if (oldpsd === "" && newpsd === "") {
            alert("请输入新密码！");
            return false;
        } else if (oldpsd != newpsd) {
            alert("两次输入的密码请保持一致！");
            return false;
        } else if (oldpsd === newpsd) {
            $.ajax({
                url: testurl + "users/update_password",
                type: "post",
                headers: {"Authorization": "Bearer " + token2},
                contentType: "application/json",
                data: JSON.stringify({
                    "id": user_idd,
                    "password": newpsd
                }),
                success: function (data) {
                    alert(data.message);
                    window.location.reload();
                }
            })
        }
    });

    $("#setgo").click(function () {
        // //上传头像
        // let head_pic = "";
        // // if ($("#changePic").val() === "2"){
        // var formdata = new FormData();
        // var pic = document.getElementById("sethead_pic").files;
        // for (var j = 0; j < pic.length; j++) {
        //     formdata.append("" + j + "", pic[j]);
        // }
        // $.ajax({
        //     async: false,
        //     url: testurl + "users/headpic",
        //     type: "post",
        //     data: formdata,
        //     cache: false,
        //     contentType: false,
        //     processData: false,
        //     success: function (data) {
        //         head_pic = data.obj;
        //     }
        // });

        let address = $("#setrealname").val();
        let phone = $("#setphone").val();
        let tel = $("#setusername").val();
        let home_tel = $("#sethome_tel").val();
        let zip_code = $("#setzip_code").val();
        let remark = $("#setremark").val();
        let presasg = $("#setpresasg").val();
        let companyId = $("#set_dw").val();
        let post_id = $("#set_sf").val();
        $.ajax({
            async: false,
            url: testurl + "users/update",
            type: "post",
            headers: {"Authorization": "Bearer " + token2},
            contentType: "application/json",
            data: JSON.stringify({
                "id": user_idd,
                "account":tel,//账号
                "name":address,//姓名
                "companyId":companyId,//单位id
                "position":presasg,//职位
                "contacts_phone":phone,//联系方式
                "post_id":post_id//
            }),
            success: function (data) {
                alert(data.message);
                window.location.reload();

            }
        })
    })


});

function autoLeftNav() {
    $('.tpl-header-switch-button').on('click', function () {
        if ($('.left-sidebar').is('.active')) {
            if ($(window).width() > 1024) {
                $('.tpl-content-wrapper').removeClass('active');
            }
            $('.left-sidebar').removeClass('active');
        } else {

            $('.left-sidebar').addClass('active');
            if ($(window).width() > 1024) {
                $('.tpl-content-wrapper').addClass('active');
            }
        }
    });
    if ($(window).width() < 1024) {
        $('.left-sidebar').addClass('active');
    } else {
        $('.left-sidebar').removeClass('active');
    }
}

// // 侧边菜单
// $('.sidebar-nav-sub-title').on('click', function () {
//     $(this).siblings('.sidebar-nav-sub').slideToggle(80)
//         .end()
//         .find('.sidebar-nav-sub-ico').toggleClass('sidebar-nav-sub-ico-rotate');
// });

// 退出系统
function exit() {
    if (confirm('确定退出系统？')) {
        sessionStorage.clear();
        window.location.href = "login.html";
    }
}

//获取个人信息
function settings() {
    $.ajax({
        url: testurl + "users/detail",
        type: "post",
        headers: {"Authorization": "Bearer " + token2},
        contentType: "application/json",
        data: JSON.stringify({
            "id": user_idd
        }),
        success: function (data) {
            console.log(data)
            $("#setusername").val(data.obj.account);
            $("#setrealname").val(data.obj.name);
            // if (data.obj.sex === "0") {
            //     $("input[name='setsex']").eq(0).prop("checked", "checked");
            // } else if (data.obj.sex === "1") {
            //     $("input[name='setsex']").eq(1).prop("checked", "checked");
            // }
            //头像
            // let imgSrc = testurl + data.obj.head_pic;
            // if (data.obj.head_pic === undefined) {
            //     imgSrc = "";
            // }
            //$("#settouxiang").attr("src", imgSrc);
            $("#setorgan").val(data.obj.company_name);
            $("#setshenfen").val(data.obj.post_name);
            $("#set_sf").val(data.obj.post_id);
            $("#set_dw").val(data.obj.companyId);
            $("#setpresasg").val(data.obj.position);
            $("#settuan").val(data.obj.deputy_team_name);
            $("#setaddress").val(data.obj.address);
            $("#settel").val(data.obj.tel);
            $("#sethome_tel").val(data.obj.home_tel);
            $("#setphone").val(data.obj.contacts_phone);
            $("#setzip_code").val(data.obj.zip_code);
            $("#setremark").val(data.obj.remark);
        }
    })
}